package cn.qiao.dubbo.provider.impl;

import cn.qiao.dubbo.HelloService;
import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.classmate.GenericType;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

@DubboService(group = "3")
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "sayHello: " + name;
    }

    @Override
    public CompletableFuture<String> sayHelloAsync(String name) {
        CompletableFuture<String> future = new CompletableFuture<>();
        new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.complete("sayHelloAsync: " + name);
        }).start();

        return future;
    }

    @Override
    public CompletableFuture<Object> sayHelloAsyncComplex(String name) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("name","小明");
        map.put("age","18");
        CompletableFuture<Object> future = new CompletableFuture<>();
        new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.complete(map);
        }).start();

        return future;
    }

    @Override
    public CompletableFuture<GenericType<Object>> sayHelloAsyncGenericComplex(String name) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("name","小明");
        map.put("age","18");
        CompletableFuture<GenericType<Object>> future = new CompletableFuture<>();
        GenericType genericType = new GenericType() {
            @Override
            public String getTypeName() {
                return null;
            }
        };
        new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.complete(genericType);
        }).start();

        return future;
    }
}